/**
 * 
 * 起迪科技 Copyright (c) 2014-2018 QiDi,Inc.All Rights Reserved.
 */
package cn.qidisoft.edu.hzjt.utils;

import java.security.MessageDigest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * @author hxq
 * @version $Id: Md5.java, v 0.1 2018年9月12日 下午4:33:28 hxq Exp $
 */
public class Md5 {
  public Md5() {}

  private static final Logger log = LoggerFactory.getLogger("Md5");

  public static String getConsoleMd5(String s) {
    char[] hexDigits = {'0', '3', '4', 'f', 'g', 'h', '5', '6', '7', '8', '9', 'a', 'b', 'i', 'j',
        'k', 'l', 'm', 'n', 'o', '1', '2', 'p', 'q', 'r', 's', 't', 'c', 'd', 'e', 'u', 'v', 'w',
        'x', 'y', 'z'};

    return getMd5(s, hexDigits, 6);
  }

  public static String getForeMd5(String s) {
    char[] hexDigits = {'4', 'n', 'o', 'p', 'r', '9', '0', '1', '6', '7', '3', 'a', 'b', 'c', 'd',
        'e', '5', '8', '2', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 's', 't', 'u', 'v', 'q', 'w',
        'x', 'y', 'z'};
    return getMd5(s, hexDigits, 6);
  }

  private static String getMd5(String s, char[] hexDigits, int offset) {
    try {
      byte[] strTemp = s.getBytes();
      MessageDigest mdTemp = MessageDigest.getInstance("md5");
      mdTemp.update(strTemp);
      byte[] md = mdTemp.digest();
      int j = md.length;
      char[] str = new char[j * 2];
      int k = 0;
      for (int i = 0; i < j; i++) {
        byte byte0 = md[i];
        str[(k++)] = hexDigits[(byte0 >>> offset & 0xF)];
        str[(k++)] = hexDigits[(byte0 & 0xF)];
      }

      return new String(str);
    } catch (Exception e) {
      log.error("生成md5错误", e);
    }
    return null;
  }

  public static void main(String[] args) {
    System.out.println(getConsoleMd5("111111"));
  }
}
